package com.zheng.oa.admin.controller;

import com.zheng.common.base.BaseController;
import com.zheng.hr.rpc.api.HrDictService;
import com.zheng.hr.rpc.api.HrPositionService;
import com.zheng.oa.common.constant.enums.OaStatus;
import com.zheng.upms.dao.model.UpmsUser;
import com.zheng.upms.rpc.api.UpmsApiService;
import com.zheng.wc.dao.model.WcNotice;
import com.zheng.wc.rpc.api.WcNoticeUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: xinqi
 * Date: 2018/4/17
 * Time: 15:07
 */
@RestController
@Api(value = "oa接口", description = "oa接口")
@RequestMapping("/oa/api")
public class OaApiController extends BaseController {
    @Autowired
    private UpmsApiService upmsApiService;
    @Autowired
    private HrPositionService positionService;
    @Autowired
    private UpmsApiService apiService;
    @Autowired
    private HrDictService dictService;
    @Autowired
    private WcNoticeUserService noticeUserService;

    @ApiOperation(value = "获取所有岗位与用户树")
    @RequestMapping(value = "/getAllPositionAndUserTree", method = RequestMethod.GET)
    public Object getAllPositionAndUserTree(
            @RequestParam(required = false, value = "isUser", defaultValue = "false") Boolean isUser,
            @RequestParam(required = false, value = "isDataScope", defaultValue = "false") Boolean isDataScope) {
        String username = null;
        if (isDataScope) {
            username = getUsername();
        }
        return positionService.getAllPositionAndUserTree(isUser, username);
    }
    @ApiOperation(value = "获取所有部门与用户树")
    @RequestMapping(value = "/selectUpmsOrganizationAndUserTree", method = RequestMethod.GET)
    public Object selectUpmsOrganizationAndUserTree(
            @RequestParam(required = false, value = "isUser", defaultValue = "false") Boolean isUser,
            @RequestParam(required = false, value = "isDataScope", defaultValue = "false") Boolean isDataScope) {
        String username = null;
        if (isDataScope) {
            username = getUsername();
        }
        return upmsApiService.selectUpmsOrganizationAndUserTree(isUser, username);
    }

    @ApiOperation(value = "获取所有通知信息")
    @RequestMapping(value = "/selectAllNotice", method = RequestMethod.GET)
    public Object selectAllNotice() {
        UpmsUser user = apiService.selectUpmsUserByUsername(getUsername());
        Integer userId = user.getUserId();
        WcNotice notice = new WcNotice();
        String state = dictService.getDictValue("已发布", "notice_state", "1");
        if (StringUtils.isNotBlank(state)) {
            notice.setState(Integer.parseInt(state));
        }
        notice.setStatus(OaStatus.NORMAL.getKey());
        List<Map<String, Object>> maps = noticeUserService.selectUserNoticePages(notice, userId);
        return maps;
    }

}
